var express = require('express');
var router = express.Router();
const shopsService = require('../service/shopsService')
// 查询
router.get('/', async function (req, res, next) {
  let { id,current, size } = req.query;
  let searchObj = {};
  if (id) {
    searchObj.adminId = id
  }
  let data = await shopsService.findAll(searchObj
    , { current: parseInt(current) || 1, size: parseInt(size) || 4 });
  res.send(data)
});

// 查询
router.get('/find', async function (req, res, next) {
  let { type, value, current, size, applytype } = req.query;
  let searchObj = {
    applytype
  };
  // 模糊查询
  if (type && value) {
    if (type == 'isAvail') {
      searchObj[type] = value;
    } else {
      searchObj[type] = { $regex: value };
    }
  }
  let data = await shopsService.findAll(searchObj
    , { current: parseInt(current) || 1, size: parseInt(size) || 4 });
  res.send(data)
});

//根据id查询
router.get('/:id', async function (req, res) {
  let id = req.params.id;
  let data = await shopsService.findById(id);
  res.send(data);
})

//插入
router.post('/', async function (req, res) {
  let data = await shopsService.insert(req.body);
  res.send(data)
  console.log(1234);
}
)

//修改
router.put('/:id', async function (req, res) {
  console.log(123);
  let id = req.params.id;
  let data = await shopsService.update(id, req.body);
  res.send(data);
})

//删除
router.delete('/:id', async function (req, res) {
  let id = req.params.id;
  let data = await shopsService.delete(id);
  res.send(data);

})

module.exports = router;
